<% group_admin = @group.is_admin?(current_user) %>
<%= list(@group.members.includes(:user).order('users.username'),
         :new_link => (group_admin ? link_to_function(icon('plus', 'Add Member...'), 'new_group_member_dialog()', :remote => true) : nil),
         :toolbar  => {:list_name => 'group_member_list',
                       :sort_by   => [%w(Username username), ['Full Name', 'full_name'], %w(Email email), %w(Added added), ['Last Sign-In', 'last_sign_in'], %w(Admin admin)],
                       :filter_by => %w(username full_name email)}) do |builder, member| %>
  <% user = member.user
     full_name = user.name %>
  <% builder.itemid "group_member_#{member.id}" %>
  <% builder.attributes raw(%(email="#{user.email}" username="#{user.username}" full_name="#{user.name}" added="#{user.created_at}" last_sign_in="#{user.current_sign_in_at}" admin="#{'admin' if member.admin?}")) %>
  <% builder.target link_to_function('', '') %>
  <% builder.top do %>
    <span class="topname"><%= user.username %></span>
    <span class="topdesc"><%= user.email %></span>
  <% end %>
  <% builder.middle full_name if full_name.present? %>
  <% builder.bottom do %>
    added <b><%= time_ago_in_words(member.created_at) %></b> by <b><%= member.created_by %></b>
    &nbsp;|&nbsp; last sign in: <b><%= user.current_sign_in_at ? time_ago_in_words(user.current_sign_in_at) : 'N/A' %></b>
  <% end %>
  <% builder.status do %>
    <%= marker('admin', 'label-info') if member.admin? %>
  <% end %>
  <% if group_admin && (member.user_id != current_user.id || current_user.is_global_admin?) %>
    <% builder.menu do %>
      <%= link_to(member.admin? ? icon('level-down', 'Revoke admin') : icon('level-up', 'Grant admin'), account_group_member_path(@group, user.id),
                  :method => :put,
                  :remote => true) %>
      <%= link_to(icon('times', 'Remove'), account_group_member_path(@group, user.id),
                  :method  => :delete,
                  :remote  => true,
                  :confirm => "Remove user '#{user.username}' from group '#{@group.name}'?") %>
    <% end %>
  <% end %>
<% end %>
